<template>
    <section>
        <el-form :inline="true" :model="filters">
            <el-form-item label="员工">
                <el-input v-model="filters.username" placeholder="员工"></el-input>
            </el-form-item>
            <el-form-item>
                <el-select v-model="filters.state" clearable  placeholder="请选择状态">
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" plain @click="loadEmployee">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleAdd">新增</el-button>
            </el-form-item>
        </el-form>
        <!--列表
        :data:准备的列表数据
        v-loading="listLoading"加载框状态
        @selection-change="selsChange"：选中复选框触发此函数
        :formatter="formatSex"-->
        <el-table :data="employees"  v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="100" label="序号">
            </el-table-column>
            <el-table-column prop="username" label="姓名" >
            </el-table-column>
            <el-table-column prop="email" label="邮件"   >
            </el-table-column>
            <el-table-column prop="phone" label="电话号" >
            </el-table-column>
            <el-table-column prop="password" label="密码" >
            </el-table-column>
            <el-table-column prop="age" label="年龄"  >
            </el-table-column>
            <el-table-column prop="state" label="状态" :formatter="formatSex"  >
            </el-table-column>
            <el-table-column prop="parent.name" label="部门"  >
            </el-table-column>
            <el-table-column label="操作" >
                <template scope="scope">
                    <el-button  size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button  plain style="background-color: red" type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--添加和修改弹出框
   :model="addForm"双向绑定的公共前缀
   :title="dialogTitle" 弹出框名字
        :visible.sync="dialogVisible"弹出框状态
   -->
        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible"   width="30%" >
            <el-form  :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="部门" prop="username">
                    <el-input v-model="addForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" >
                    <el-input v-model="addForm.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话号" prop="phone">
                <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                 </el-form-item>
                <el-form-item label="年龄" prop="age" >
                    <el-input v-model.number="addForm.age" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="部门">
                    <el-select v-model="addForm.deptId" clearable placeholder="请选择">
                        <el-option
                                v-for="item in depts"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button plain type="primary" @click="addbutton">确 定</el-button>
      </span>
        </el-dialog>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

    </section>
</template>

<script>
    export default {
        data(){
            return{
                //弹出框名字
                dialogTitle:'',
                //弹出框状态
                dialogVisible: false,
                //添加和修改from表单
                addForm:{
                    id:'',
                    username:'',
                    email:'',
                    phone:'',
                    age:'',
                    state:'',
                    deptId:''
                },
                //表格对象
                employees:[],
                //加载框状态
                listLoading:false,
                //每页条数
                pageSize:15,
                //当前页条数
                currentPage:1,
                //总条数
                total:0,
                //选中复选框改变的参数
                sels:[],
                //部门
                depts:[],

                filters:{
                    username:"",
                    state:""
                },
                //验证
                addFormRules: {
                    username: [
                        {required: true, message: '部门必填！!', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '电话号必填！!', trigger: 'blur'}
                    ],
                    age: [
                        {required: true, type: 'number' ,message: '电话号必填！!', trigger: 'blur'}
                    ]
                }
            }
        },
        methods:{
            //提交按钮
            addbutton(){
                //动态添加部门属性
                if (this.addForm.deptId){
                    this.addForm.parent={"id":this.addForm.deptId}
                }
                this.$refs["addForm"].validate((valid=>{
                    if(valid){
                        this.$http.put("/employee",this.addForm).then((res=>{
                            console.debug(res)
                            let {success,msg}=res.data
                            if (success){
                                this.$notify({
                                    title: '提示',
                                    message: '操作成功',
                                    type: 'success'
                                });
                                //刷新页面
                                this.loadEmployee();
                                this.loadDepartment();
                                this.dialogVisible=false
                            }else {
                                this.$message({
                                    type:'msg',
                                    message:'操作失败'
                                })
                                this.$http.error(msg)
                            }
                        }))

                    }
                }))
            },
            //删除
            handleDel(index,row){
                 this.$confirm("'确认删除该记录吗?', '提示',",{
                     confirmButtonText: '确定',
                     cancelButtonText: '取消',
                     type:'warning'
                 }).then(()=>{
                     this.$http.delete("/employee/"+row.id).then((res=>{
                         let {success,msg}=res.data
                         if (success){
                             this.$notify.info({
                                 title: '提示',
                                 message: '删除成功'
                             });
                             //刷新页面
                             this.loadEmployee();
                         }else {
                             this.$http.error(msg)
                         }
                     }))
                 })

            },
            //格式化指定列的值  row:当前行对应的值   column:列属性的介绍
            formatSex(row,colum){
                return row.state?"启用":"禁用";
            },
            //新增
            handleAdd(){
                //修改框名字
                this.dialogTitle='新增人员'
                if(this.$refs["addFrom"]){
                    //重重验证
                    this.$refs["addFrom"].resetFields();
                }
                //清空框内数据
                for (var k in this.addForm){
                    this.addForm[k]=''
                }
                //默认的状态
                this.addForm.state=1
                //弹出框状态
                this.dialogVisible=true
            },
            //修改
            handleEdit(index,row){
                console.debug(row)
                //修改框名字
                this.dialogTitle='修改人员'

                //回显数据
                for (var k in this.addForm){

                    this.addForm[k]=row[k]
                }
                //回显部门信息
                if (row.parent){
                    this.addForm.deptId=row.parent.id
                }
                //弹出框状态
                this.dialogVisible=true

            },
            selsChange(vals){
               // console.debug(vals)
                this.sels=vals

            },
            //加载员工表
            loadEmployee(){
                //加载框状态
                this.listLoading=true
                //准备分页数据
                let param={"currentPage":this.currentPage,"pageSize":this.pageSize ,"username":this.filters.username,
                "state":this.filters.state}
                this.$http.patch("/employee",param).then((res=>{
                   // console.debug(res)
                    this.$notify({
                        title: '提示',
                        message: '已显示全部信息',
                    });
                    let {total,result}=res.data
                    this.total=total
                    this.employees=result
                   this.listLoading=false
                }))
            },
            //批量删除
            batchRemove(){
                this.$confirm('确认删除该记录吗?', '提示',{
                    type:"warning"
                }).then(()=>{
                    this.$http.patch("/employee/batchRemove",this.sels).then((res=>{
                        let {success,msg}=res.data
                        if(success){
                            //刷新表
                            this.loadEmployee();
                        } else {
                            this.$http.error(msg)
                        }
                        //console.debug(res)
                    }))
                })

            },
            //页码发生改变的时候触发此函数     @param  val:页码
            handleCurrentChange(val){
                this.currentPage=val
                this.loadEmployee();
            },
            //查询全部部门
            loadDepartment(){
                this.$http.patch("/department/loadAll").then((res=>{
                    this.depts=res.data
                }))
            }
        },
        mounted(){
            this.loadEmployee();
            this.loadDepartment();
        }
    }

</script>

